package cc.leap.web;

import cc.leap.helper.LoginHelper;
import cc.leap.model.AjaxResult;
import cc.leap.model.LoginUser;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysRole;
import cc.leap.model.domain.SysUser;
import cc.leap.model.domain.SysUserRole;
import cc.leap.service.SysLoginService;
import cc.leap.service.SysRoleService;
import cc.leap.service.SysUserService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.java.Log;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.validation.annotation.Validated;

/**
 * SysRoleController
 *
 * @author leap
 * @date 2024/8/19
 */
@Controller
@Mapping("/system/role")
public class SysRoleController {

    @Inject
    SysRoleService roleService;

    @Inject
    SysUserService userService;

    @Inject
    SysLoginService loginService;

    @SaCheckPermission("system:role:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysRole> list(SysRole role) {
        return roleService.selectRolePage(role);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @SaCheckPermission("system:role:query")
    @Get
    @Mapping(value = "/{roleId}")
    public AjaxResult getInfo(Long roleId) {
        return AjaxResult.success(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @SaCheckPermission("system:role:add")
    @Post
    @Mapping
    public AjaxResult add(@Validated SysRole role) {
        if (!roleService.checkRoleNameUnique(role)) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (!roleService.checkRoleKeyUnique(role)) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setCreateBy(LoginHelper.getUsername());
        return AjaxResult.success(roleService.insertRole(role));
    }

    /**
     * 修改保存角色
     */
    @SaCheckPermission("system:role:edit")
    @Put
    @Mapping
    public AjaxResult edit(@Validated SysRole role) {

        if (!roleService.checkRoleNameUnique(role)) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (!roleService.checkRoleKeyUnique(role)) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setUpdateBy(LoginHelper.getUsername());

        if (roleService.updateRole(role) > 0) {
            // 更新缓存用户权限
            LoginUser loginUser = LoginHelper.get();
            if (ObjectUtil.isNotNull(loginUser.getUser()) && !LoginHelper.isSuperAdmin(loginUser.getUser().getUserId())) {
                loginUser.setPermissions(loginService.getMenuPermission(loginUser.getUser()));
                loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
                LoginHelper.set(loginUser);
            }
            return AjaxResult.success();
        }
        return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，请联系管理员");
    }

    /**
     * 删除角色
     */
    @SaCheckPermission("system:role:remove")
    @Delete
    @Mapping("/{roleIds}")
    public AjaxResult remove(Long[] roleIds) {
        return AjaxResult.success(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 查询已分配用户角色列表
     */
    @SaCheckPermission("system:role:list")
    @Get
    @Mapping("/authUser/allocatedList")
    public TableDataInfo<SysUser> allocatedList(SysUser user) {
        return userService.selectAllocatedPage(user);
    }

    /**
     * 取消授权用户
     */
    @SaCheckPermission("system:role:edit")
    @Put
    @Mapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(SysUserRole userRole) {
        return AjaxResult.success(roleService.deleteAuthUser(userRole));
    }


}
